package com.springboot2.controller;

import com.springboot2.entity.Information;
import com.springboot2.service.InformationService;
import com.springboot2.utils.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("information/")
public class InformationController {

    @Resource
    private InformationService informationService;

    //新
    @PostMapping("add")
    public Result save(@RequestBody Information information){
        informationService.add(information);
        return Result.success();
    }

    //删
    @DeleteMapping("del/{id}")
    public Result del(@PathVariable int id){
        informationService.del(id);
        return Result.success();
    }

    //改
    @PutMapping("update")
    public Result update(@RequestBody Information information){
        informationService.update(information);
        return Result.success();
    }
    //查
    @GetMapping("findById/{id}")
    public Result findById(@PathVariable int id){
        return Result.success(findById(id));
    }
    //查-列表
    @GetMapping("list/{pageNo}/{pageSize}")
    public Result findByPage(@PathVariable int pageNo,@PathVariable int pageSize){
        Map<String,Object> map = new HashMap<>();
        map.put("data",informationService.findByPage(pageNo,pageSize));
        map.put("total",informationService.count());
        return Result.success(map);
    }

    //查-列表 -查询
    @GetMapping("searchList/{pageNo}/{pageSize}/{title}")
    public Result findByPageParams(@PathVariable int pageNo,@PathVariable int pageSize,@PathVariable(required = false) String title){
        Map<String,Object> map = new HashMap<>();
        map.put("data",informationService.findByPageParams(pageNo,pageSize,title));
        map.put("total",informationService.countParams(title));
        return Result.success(map);
    }

}
